//
//  BaseCornerCollectionViewCell.swift
//  app
//
//  Created by g1 on 2023/9/5.
//

import UIKit

open class BaseCornerCollectionViewCell: UICollectionViewCell {
    
    /// 背景颜色
    public var cornerBackColor: UIColor = UIColor.white {
        didSet {
            noneCornerView.backgroundColor = cornerBackColor
            allCornerView.backgroundColor = cornerBackColor
            topCornerView.backgroundColor = cornerBackColor
            bottomCornerView.backgroundColor = cornerBackColor
        }
    }
    /// 左右间距
    public var backHorizontalMargin: CGFloat = 16 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.left.equalTo(backHorizontalMargin)
                make.right.equalTo(-backHorizontalMargin)
            }
        }
    }
    /// 上下间距
    public var backVerticalMargin: CGFloat = 0 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.top.equalTo(backVerticalMargin)
                make.bottom.equalTo(-backVerticalMargin)
            }
        }
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        subLayout()
        
        updateRadius()
    }
    
    public func updateRadius() {
        
        contentView.layoutIfNeeded()
        // 设置圆角
        DispatchQueue.main.async {
            self.topCornerView.layerCornerRadius(8, [.topLeft,.topRight])
            self.bottomCornerView.layerCornerRadius(8, [.bottomLeft,.bottomRight])
            self.allCornerView.layerCornerRadius(8, [.allCorners])
        }
        
    }
    
    // 设置cell圆角
    public func setCorner(direction: AppCellRectCorner) {
        
        topCornerView.isHidden = !(direction == .top)
        bottomCornerView.isHidden = !(direction == .bottom)
        allCornerView.isHidden = !(direction == .all)
        noneCornerView.isHidden = !(direction == .none)
        
    }
    
    // MARK: - configUI
    func subLayout() {
        
        backgroundColor = .clear
        
        noneCornerView.backgroundColor = cornerBackColor
        allCornerView.backgroundColor = cornerBackColor
        topCornerView.backgroundColor = cornerBackColor
        bottomCornerView.backgroundColor = cornerBackColor
        
        contentView.addSubview(noneCornerView)
        contentView.addSubview(allCornerView)
        contentView.addSubview(topCornerView)
        contentView.addSubview(bottomCornerView)

        noneCornerView.snp.makeConstraints { make in
            make.left.equalTo(backHorizontalMargin)
            make.right.equalTo(-backHorizontalMargin)
            make.top.equalTo(backVerticalMargin)
            make.bottom.equalTo(-backVerticalMargin)
        }
        allCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        topCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        bottomCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        
    }
    private(set) lazy var noneCornerView = UIView()
    private(set) lazy var allCornerView = UIView()
    private(set) lazy var topCornerView = UIView()
    private(set) lazy var bottomCornerView = UIView()
        
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
